package edu.cmu.vlis.datamining.SimilarityFeatures;

import java.util.HashSet;
import java.util.Set;

import org.jgrapht.alg.NeighborIndex;

import edu.cmu.vlis.datamining.core.Edge;
import edu.cmu.vlis.datamining.core.Feature;
import edu.cmu.vlis.datamining.core.IFeatureCalculator;
import edu.cmu.vlis.datamining.core.Vertex;

public class JaccardCoefficient implements IFeatureCalculator{

    private NeighborIndex<Vertex, Edge> nIndex;
    private int featureId;
    
    public JaccardCoefficient(NeighborIndex<Vertex, Edge> neighborIndex) {
    
        this.nIndex = neighborIndex;
        this.featureId = 2;
    }
public Feature compute(Vertex a, Vertex b){
        
        Set<Vertex> edgesA = new HashSet<Vertex>(nIndex.neighborsOf(a));
        Set<Vertex> edgesB = nIndex.neighborsOf(b);
        Set<Vertex> edgesACopy = new HashSet<Vertex>(edgesA);
               
        edgesA.retainAll(edgesB);
        edgesACopy.addAll(edgesB);
        
        return new Feature(this.featureId,((float)edgesA.size() / (float)edgesACopy.size())); 
        
    }
}
